package com.tjetc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageInfo;
import com.tjetc.domain.Hospital;
import com.tjetc.service.HospitalService;
import com.tjetc.util.AliYunOss;
import com.tjetc.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * (hospital)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/hospital")
public class HospitalController {
    /**
     * 服务对象
     */
    @Resource
    private HospitalService hospitalService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Hospital selectOne(@RequestParam("id") Integer id) {
        return hospitalService.selectByPrimaryKey(id);
    }
    @GetMapping("getHos")
    public Result getHos(@RequestParam("id") Integer id) {
        return hospitalService.selectByIdWithDepartments(id);
    }

    @GetMapping("selHosByPage")
    public Result selHosByPage(@RequestParam("page") Integer curPage) {
        System.out.println(curPage);
        return hospitalService.selHosByPage(curPage);
    }

@RequestMapping("listHospital")
    public List<Hospital> listHospital(
            @RequestParam("name")String name,
            @RequestParam("address")String address
            ){
    return hospitalService.listHospital(name,address);
}


    @GetMapping("findByName")
    public Result findByName(@RequestParam("name") String name){
        System.out.println(name);
        return hospitalService.findByName(name);
    }

    @GetMapping("findByHosDep")
    public Result findByHosDep(
            @RequestParam(value = "hosInput",defaultValue = "") String hosInput,
            @RequestParam(value = "depInput",defaultValue = "") String depInput
    ){
        return hospitalService.findByHosDep(hosInput,depInput);
    }
    @GetMapping("selAll")
    public Result selAll(@RequestParam("sgId") Integer sgId,@RequestParam("location") String location,@RequestParam("pageSize")Integer pageSize){
        return hospitalService.selAll(sgId,location,pageSize);
    }

    @GetMapping("getCountHos")
    public Result getCountHos(){
        return hospitalService.getCountHos();
    }

    @GetMapping("getAllDistrict")
    public Result getAllDistrict(){
        return hospitalService.getAllDistrict();
    }

    @GetMapping("getHosByDis")
    public Result getHosByDis(
            @RequestParam(value = "district" ,defaultValue = "") String district,
            @RequestParam(value = "level",defaultValue = "") String level
    ){
        return hospitalService.getHosByDis(district,level);
    }
    @GetMapping("selByPage")
    public Result selByPage(
            @RequestParam(value = "curPage",defaultValue = "1") Integer curPage,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name",defaultValue = "") String name
    ){
        return hospitalService.selByPage(curPage,pageSize,name);
    }

    @PostMapping("save")
    public Result save(@RequestParam(value = "id",required = false) Integer id,
                       @RequestParam("name") String name,
                       @RequestParam("address") String address,
                       @RequestParam("typeId") Integer typeId,
                       @RequestParam(value = "desc",required = false) String desc,
                       @RequestParam(value = "guidance",required = false) String guidance,
                       @RequestParam(value = "prompt",required = false) String prompt,
                       @RequestParam(value = "icon",required = false) String icon,
                       @RequestParam("isAlternate") Integer isAlternate,
                       @RequestParam("x") double x,
                       @RequestParam("y") double y,
                       @RequestParam(value = "photo",required = false) MultipartFile photo) throws IOException, ClientException {
        Hospital hospital = Hospital.builder().id(id).name(name).address(address).typeId(typeId).desc(desc).guidance(guidance).prompt(prompt)
                .icon(icon).isAlternate(isAlternate).x(x).y(y).build();
        if (id == null){
            if (photo != null && !photo.isEmpty()) {
                String iconStr = AliYunOss.upload(photo.getBytes(), photo.getOriginalFilename());
                hospital.setIcon(iconStr);
                return hospitalService.add(hospital);
            }else {
                return Result.fail("图片为空");
            }
        }else {
            if (photo != null && !photo.isEmpty()) {
                String iconStr = AliYunOss.upload(photo.getBytes(), photo.getOriginalFilename());
                hospital.setIcon(iconStr);
                return hospitalService.update(hospital);
            }else {
                return hospitalService.update(hospital);
            }
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("id") Integer id){
        return hospitalService.delete(id);
    }
    @GetMapping("forceDel")
    public Result forceDel(@RequestParam("id") Integer id){
        return hospitalService.forceDel(id);
    }

    @GetMapping("selAllhospital")
    public Result selAllhospital(){
        return hospitalService.selAllhospital();
    }

    @RequestMapping("getAllByName")
    public PageInfo<Hospital> getAllByName(
            @RequestParam("curPage") Integer curPage,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("name") String name
    ){
        return hospitalService.getAllByName(curPage,pageSize,name);
    }

    @RequestMapping("addHos")
    public Result addHos(@RequestBody Hospital hospital){
        return hospitalService.addHos(hospital);
    }

    @RequestMapping("delHos")
    public Result delHos(
            @RequestParam("id") Integer id
    ){
        return hospitalService.deleteById(id);
    }

    @RequestMapping("updateHos")
    public Result updateHos(
            @RequestParam("id") Integer id,
            @RequestParam("desc") String desc,
            @RequestParam("guidance") String guidance,
            @RequestParam("prompt") String prompt
    ){
        return hospitalService.updateHos(id,desc,guidance,prompt);
    }
}
